package fun.qu_an.velocity.whitelist;

import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.event.permission.PermissionsSetupEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyReloadEvent;
import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.UuidUtils;
import fun.qu_an.velocity.whitelist.command.VWhitelistCommand;
import fun.qu_an.velocity.whitelist.whitelist.WhitelistManagements;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.nio.file.Path;

import static fun.qu_an.velocity.whitelist.command.VWhitelistCommand.PERMISSION_WHITELIST_ADMIN;
import static fun.qu_an.velocity.whitelist.command.VWhitelistCommand.PERMISSION_WHITELIST_USE;
import static fun.qu_an.velocity.whitelist.config.VelocityWhitelistConfig.CONFIG;
import static fun.qu_an.velocity.whitelist.translate.Translates.KICK_NOT_IN_WHITELIST;
import static fun.qu_an.velocity.whitelist.translate.Translates.LANGUAGE_LOADER;

@Plugin(
	id = BuildConstants.PLUGIN_ID,
	name = BuildConstants.PLUGIN_NAME,
	version = BuildConstants.VERSION,
	authors = "Harvey_Husky"
)
public class VelocityWhitelist {
	public static final Path DATA_DIRECTORY = Path.of("plugins").resolve(BuildConstants.PLUGIN_NAME);
	private static ProxyServer proxyServer;
	private static VelocityWhitelist instance;
	private static Logger logger;

	public static Logger getLogger() {
		return logger;
	}

	public static VelocityWhitelist getInstance() {
		return instance;
	}

	private static void reload() {
		LANGUAGE_LOADER.loadOrReload(); // 1
		CONFIG.load(); // 2
		WhitelistManagements.load(); // 3
	}

	@Inject
	public VelocityWhitelist(ProxyServer proxyServer, Logger logger) {
		VelocityWhitelist.proxyServer = proxyServer;
		VelocityWhitelist.logger = logger;
		VelocityWhitelist.instance = this;
	}

	@Contract(pure = true)
	public static ProxyServer getProxyServer() {
		return proxyServer;
	}

	@Subscribe
	public void onProxyInitialization(ProxyInitializeEvent event) {
		reload(); // 1、2、3
		VWhitelistCommand.register(); // 4
		logger.info("§a" + BuildConstants.PLUGIN_NAME + " v" + BuildConstants.VERSION + " 已加载！");
	}

	@Subscribe
	public void onProxyReload(ProxyReloadEvent event) {
		reload();
	}

	@Subscribe
	public void onPlayerPostLogin(@NotNull PostLoginEvent event) {
		Player player = event.getPlayer();
		logger.info(UuidUtils.toUndashed(player.getUniqueId()));
		if (CONFIG.isEnableWhitelist()
			&& !WhitelistManagements.checkAndUpdate(player)) {
			player.disconnect(KICK_NOT_IN_WHITELIST);
		}
	}

	@Subscribe
	public void onPermissionSetup(@NotNull PermissionsSetupEvent event) {
		if (!(event.getSubject() instanceof Player player)) return;
		// 获取原权限函数
		PermissionFunction function = event.createFunction(player);
		// 设置新权限函数
		event.setProvider(subject -> permission -> {
			if (permission.equals(PERMISSION_WHITELIST_ADMIN)) {
				return Tristate.fromBoolean(CONFIG.hasAdminPermission(player.getUsername()));
			}
			if (permission.equals(PERMISSION_WHITELIST_USE)) {
				return Tristate.fromBoolean(CONFIG.hasUsePermission(player.getUsername()));
			}
			return function.getPermissionValue(permission);
		});
	}
}
